package com.oa.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.oa.mapper.user.LogMapper;
import com.oa.mapper.user.UserMapper;
import com.oa.pojo.Emp;
import com.oa.pojo.Log;
import com.oa.service.user.LogService;
import com.oa.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author: 一拳超人
 * @Date: 2021/10/10 17:19
 */
@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogMapper logMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageResult getOneselfList(PageResult pageResult) {
        IPage<Log> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasLength(pageResult.getQuery()), "title", pageResult.getQuery())
                .eq("emp_id", pageResult.getEmpId());
        page = logMapper.selectPage(page, queryWrapper);
        return pageResult.setRows(page.getRecords()).setTotal(page.getTotal());
    }

    @Override
    public String getLogAuthorInfo(String empId) {
        Emp emp = userMapper.selectOne(new QueryWrapper<Emp>().eq("emp_id", empId));
        return emp.getEmpName();
    }

    @Override
    public Log getLogInfo(Log log) {
        return logMapper.selectById(log);
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public String addNewLog(Log log) {
        int i = logMapper.insert(log);
        return i != 0 ? "成功" : "失败";
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public String deleteLog(Log log) {
        int i = logMapper.deleteById(log.getId());
        return i != 0 ? "成功" : "失败";
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public String updateLogInfo(Log log) {
        int i = logMapper.updateById(log);
        return i != 0 ? "成功" : "失败";
    }
}
